package com.example.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author zty
 * @apiNote rabbitmq配置
 * @user nav1c
 * @date 2023/12/14 10:58
 * @project study-cloud
 */
@Configuration
public class RabbitMQConfig {

    @Value("${spring.rabbitmq.template.exchange}")
    private String exchange;

    @Value("${spring.rabbitmq.template.default-receive-queue}")
    private String defaultReceiveQueue;

    /**
     * 队列，如果不存在则会创建
     * @return 队列
     */
    @Bean
    public Queue defaultReceiveQueue() {
        return new Queue(defaultReceiveQueue, true);
    }

    /**
     * 交换机 如果不存在则会创建 <br>
     * {@link Exchange} 有多种实现类
     * @return 交换机
     */
    @Bean
    public FanoutExchange exchange() {
        return new FanoutExchange(exchange, true, true);
    }

    /**
     * 将队列绑定到中心交换机
     * @return 绑定
     */
    @Bean
    public Binding binding(Queue queue, FanoutExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange);
    }

//    @Bean
//    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(@Autowired ConnectionFactory connectionFactory){
//        final SimpleRabbitListenerContainerFactory containerFactory = new SimpleRabbitListenerContainerFactory();
//        containerFactory.setConnectionFactory(connectionFactory);
//        containerFactory.setMessageConverter(new Jackson2JsonMessageConverter());
//        return containerFactory;
//    }
//
//    @Bean
//    public RabbitTemplate rabbitTemplate(@Autowired ConnectionFactory connectionFactory) {
//        final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
//        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
//        return rabbitTemplate;
//    }

}
